<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>


  <script>
    //this 运行在哪个对象下，就指向哪个对象
    k = '678';
    function fun() {
      var k = '789';
      console.log(this.k);
    }

    var o1 = {
      k: "123",
      f: fun,
    }

    var o2 = {
      k: "234",
      f: fun,
    }

    o1.f();//输出123
    o2.f();//输出234
    console.log(writeLine(6, ''));
 
    var o11={
      age:18,
      fun:function(){
        console.log(this.age);
      }
    }

    var o22={
      age:16,
      fun:o11.fun,
    }
    o22.fun();//o22对象调用，所以this指向o22


    /*输出横线*/
    function writeLine(n, tip) {
      var line = '';
      for (var i = 0; i < n; i++) {
        if (i == parseInt((n / 2))) {
          line += tip + '-';
        }
        else {
          line += '-';
        }
      }
      return line;
    }
  </script>

</body>

</html>